package com.uniledger.exchange.bank;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import me.yokeyword.indexablerv.IndexableAdapter;

public class BankCardIndexAdapter extends IndexableAdapter<BankCard> {

    private LayoutInflater inflater;
    private Context context;

    public BankCardIndexAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateTitleViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.list_item_bank_index, parent, false);
        return new IndexVH(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.list_item_bank_card, parent, false);
        return new ContentVH(view);
    }

    @Override
    public void onBindTitleViewHolder(RecyclerView.ViewHolder holder, String indexTitle) {
        IndexVH vh = (IndexVH) holder;
        vh.tv.setText(indexTitle);
    }

    @Override
    public void onBindContentViewHolder(RecyclerView.ViewHolder holder, BankCard entity) {
        ContentVH vh = (ContentVH) holder;
        vh.tvName.setText(entity.bank);
        vh.tvNumber.setText(entity.getCardNumber());
        GlideApp.with(context).load(entity.bankIcon)
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)
                .into(vh.img_list_icon);
    }

    static class IndexVH extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_card_index)
        TextView tv;

        public IndexVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    static class ContentVH extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_list_name)
        TextView tvName;
        @BindView(R.id.txt_list_number)
        TextView tvNumber;

        @BindView(R.id.img_list_icon)
        CircleImageView img_list_icon;

        public ContentVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
